﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;

namespace SanctusSoftware.Text.Extraction
{

    // Ah... it's an idea

    public static class TextExtractor
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalText"></param>
        /// <param name="regexPattern">Use Capture Group Name "Text"</param>
        /// <returns></returns>
        public static string ExtractText(string originalText, string regexPattern)
        {

            Match m = Regex.Match(originalText, regexPattern);

            if (m != null)
            {
                return m.Groups["Text"].Value;
            }
            else
            {
                return string.Empty;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalText"></param>
        /// <param name="regexPatterns">Key is the name of the pattern, Value is the pattern</param>
        /// <returns></returns>
        public static Dictionary<string, string> ExtractText(string originalText, Dictionary<string, string[]> regexPatterns)
        {

            Dictionary<string, string> values = new Dictionary<string, string>();

            foreach (string key in regexPatterns.Keys)
            {
                
                // Get Possible Patterns and loop through until text is found
                string[] patterns = regexPatterns[key];
                string text = string.Empty;
                for (int i = 0; i < patterns.Length; i++)
                {
                    string pattern = patterns[i];
                    string temp = ExtractText(originalText, pattern);
                    if (!string.IsNullOrEmpty(temp))
                    {
                        text = temp;
                        break;
                    }
                }

                values.Add(key, text);
            }

            return values;

        }

        /// <summary>
        /// Extracts All Text From The Specified URL
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ExtractTextFromUrl(string url)
        {

            WebClient webClient = new WebClient();

            byte[] reqHTML;
            reqHTML = webClient.DownloadData(url);
            UTF8Encoding objUTF8 = new UTF8Encoding();

            string pageText = objUTF8.GetString(reqHTML);

            return pageText;

        }

        /// <summary>
        /// Extracts Specific Text (based on the regular expression pattern) from the url
        /// </summary>
        /// <param name="url"></param>
        /// <param name="regexPattern"></param>
        /// <returns></returns>
        public static string ExtractTextFromUrl(string url, string regexPattern)
        {
            string text = ExtractTextFromUrl(url);
            return ExtractText(text, regexPattern);
        }

    }

}
